package com.labichaoka.chaoka.ui.account;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.labichaoka.chaoka.R;
import com.labichaoka.chaoka.base.BaseActivity;
import com.labichaoka.chaoka.base.GlobalParams;
import com.labichaoka.chaoka.base.MyApplication;
import com.labichaoka.chaoka.entity.LoginRequest;
import com.labichaoka.chaoka.ui.web.WebViewActivity;
import com.labichaoka.chaoka.utils.ChannelUtil;
import com.labichaoka.chaoka.utils.DetectionUtil;
import com.labichaoka.chaoka.utils.RandomUntil;
import com.labichaoka.chaoka.utils.SMSManager;
import com.labichaoka.chaoka.utils.SendBroadCastUtil;
import com.labichaoka.chaoka.utils.ToastUtils;
import com.umeng.analytics.AnalyticsConfig;

import butterknife.BindView;
import butterknife.OnClick;


/*
* 登录
*
* */

public class LoginActivity extends BaseActivity implements LoginView{
    @BindView(R.id.phoneNum)
    EditText phoneNum;
    @BindView(R.id.edt_img)
    EditText edtImg;

    @BindView(R.id.code_btn)
    TextView codeBtn;

    @BindView(R.id.divider_line)
    View line;
    @BindView(R.id.img_layout)
    RelativeLayout imgLayout;

    @BindView(R.id.img)
    ImageView img;
    @BindView(R.id.commit)
    Button commit;

    @BindView(R.id.edit_code)
    EditText editCode;


    private LoginPresenter presenter;
    private String imgUrl=""; //获取图片验证码
    private String phoneStr;
    private String imgCodeStr;
    private String codeStr;
    private boolean isImgCode=false;//是否需要图片验证码


    @Override
    protected int initLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        presenter=new LoginPresenterImpl(this,new LoginInteractorImpl());
        SMSManager.getInstance(mContext).bindView(codeBtn);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        phoneNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                phoneStr=s.toString().trim();
                checkButtonState();
            }
        });

        edtImg.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                imgCodeStr=s.toString().trim();
                checkButtonState();
            }
        });

        editCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                codeStr=s.toString().trim();
                checkButtonState();
            }
        });
    }

    @Override
    public void showProgress() {
        showLoadingProgress();
    }

    @Override
    public void hideProgress() {
        hideLoadingProgress();
    }

    @Override
    public void navigateToHome() {
        new SendBroadCastUtil(MyApplication.mContext).sendBroad
                (GlobalParams.LOGIN_SUCCESS_ACTION, null);
        finish();
    }

    @Override
    public void showMessage(String str) {
        ToastUtils.show(str);
    }

    @Override
    public void showImgCode() {
        isImgCode=true;
        imgUrl= GlobalParams.baseUrl+"vcode/img/send"+"/"+phoneNum.getText().toString().trim()+"?"+ RandomUntil.getNum(999999);
        line.setVisibility(View.VISIBLE);
        imgLayout.setVisibility(View.VISIBLE);
        Glide.with(this).load(imgUrl).into(img);
    }

    @Override
    public void startCountDown() {
        SMSManager.getInstance(mContext).start();
    }

    @Override
    public void countDownCancel() {
        SMSManager.getInstance(mContext).cancel();
    }

    @OnClick({R.id.code_btn,R.id.img,R.id.commit,R.id.register_proto})
    public void click(View v){
        switch (v.getId()){
            case R.id.code_btn:
                if (isImgCode){
                    if (checkPhone(phoneStr)&&!TextUtils.isEmpty(imgCodeStr)){
                        presenter.getSmsCode(phoneStr,imgCodeStr);
                    }else{
                        showMessage("请输入正确的手机号和图形验证码!");
                    }
                }else{
                    if (checkPhone(phoneStr)){
                        presenter.getSmsCode(phoneStr,imgCodeStr);
                    }else{
                        showMessage("请输入正确的手机号!");
                    }
                }


                break;
            case R.id.img:
                if (checkPhone(phoneStr)){
                    imgUrl= GlobalParams.baseUrl+"vcode/img/send"+"/"+phoneStr+"?"+ RandomUntil.getNum(999999);
                    Glide.with(this).load(imgUrl).into(img);
                }else{
                    showMessage("请输入正确的手机号!");
                }
                break;
            case R.id.commit:
                LoginRequest request=new LoginRequest();
                request.setLoginPhone(phoneStr);
                request.setValiCode(codeStr);
                request.setChannelSource(ChannelUtil.getChannelInfo());
                presenter.login(request);
                break;
            case R.id.register_proto:
                Bundle bundle=new Bundle();
                bundle.putString("type","8");
                bundle.putString("url","https://cscard.labifenqi.com/loginagreement");
                gotoActivity(mContext,WebViewActivity.class,bundle);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.onDestroy();
    }

    public void checkButtonState(){
        if (isImgCode){
            //需要图片验证码
            if (!TextUtils.isEmpty(phoneStr)&& DetectionUtil.isMobileNO(phoneStr)&&!TextUtils.isEmpty(imgCodeStr)&&!TextUtils.isEmpty(codeStr)){
                commit.setEnabled(true);
            }else{
                commit.setEnabled(false);
            }
        }else{
            if (!TextUtils.isEmpty(phoneStr)&& DetectionUtil.isMobileNO(phoneStr)&&!TextUtils.isEmpty(codeStr)){
                commit.setEnabled(true);
            }else{
                commit.setEnabled(false);
            }
        }

    }

    public boolean checkPhone(String phoneNum){
        if (!TextUtils.isEmpty(phoneNum)&& DetectionUtil.isMobileNO(phoneNum)){
            return true;
        }else{
            return false;
        }
    }
}
